package com.xujc.controller;

import com.opensymphony.xwork2.ActionSupport;
import com.xujc.dao.IUserDAO;
import com.xujc.entity.UserEntity;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.Cookie;

/**
 * Created by xujc on 6/21/15.
 */
public class DologinAction extends ActionSupport {
    private String mEmail;
    private String mPassword;
    private IUserDAO mUserDAO;


    @Override
    public String execute() throws Exception {
        boolean b = mUserDAO.hasUser(mEmail) && mUserDAO.checkPassword(mEmail, mPassword);
        if (b){
            UserEntity byEmail = mUserDAO.getByEmail(mEmail);
            int uid=byEmail.getUId();
            Cookie cookie = new Cookie("uid",String.valueOf(uid));
            cookie.setMaxAge(24*24*60);
            ServletActionContext.getResponse().addCookie(cookie);
            return SUCCESS;
        } else {
            return ERROR;
        }
    }

    public String getEmail() {
        return mEmail;
    }

    public void setEmail(String email) {
        mEmail = email;
    }

    public String getPassword() {
        return mPassword;
    }

    public void setPassword(String password) {
        mPassword = password;
    }

    public void setUserDAO(IUserDAO userDAO) {
        mUserDAO = userDAO;
    }


}
